$(function () {
    var storyid = $("#storyid").val();
    var shopid = $("#shopid").val();
    // 监听滚动事件
    $(window).scroll(function () {
        if (isempty(shopid))
            return;
        // 获得div的高度
        var height = $(window).height();
        var h = $("#buy").offset().top;
        if ($(this).scrollTop() >= h - height && $(this).scrollTop() < h) {
            // 滚动到指定位置
            $("#want").hide();
        } else {
            $("#want").show();
        }
    });
    var url = "/except/shopcontroller/storyinfo.do";
    var token = $("#token").val();
    if(isempty(token))
        token="null";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            storyid: storyid,
            json:jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                var datainfo = data.data;
                //console.log(datainfo.desc);
                $("#storyname").html(datainfo.story_name);
                $("#username").html(datainfo.username);
                $("#storydesc").html(datainfo.desc);
                // 加载顶部图片
                // $("#storylogo").attr("src", datainfo.story_logo);
                $("#storylogo").append('<div style="margin-bottom: 100px; height: 240px;overflow-y: hidden; background: url('+datainfo.story_logo+') no-repeat center;background-size: cover;width: 100%;margin: 0 auto;" onerror="imgerror(this,\'img/onerror.png\');"></div>');
                //  加载顶部的图片
                //TODO

                var evaluate = datainfo.evaluate;
                var evaluateinfo = '';
                var evaluatelength = 0;
                $(evaluate).each(function () {
                    evaluatelength++;
                    var reply = this.reply;
                    var imgurl;
                    if (this.ispraise)
                        imgurl = 'img/praise_red.png';
                    else
                        imgurl = 'img/praise.png';
                    evaluateinfo += '<div style="padding: 15px;background-color: white;margin-bottom: 1px;">' +
                        '<div style="float: left;margin-top: 6px;">' +
                        '<img  src="' + this.headimg + '" class="item_head_img" onerror="imgerror(this, \'img/onerror.png\');"/>' +
                        '</div>' +
                        '<div style="margin-left: 10px;">' +
                        '<div>' +
                        '<span class="left">' + this.nickname + '</span>' +
                        '<div class="right">' +
                        '<span id="praise' + this.evaluate_id + '">' + this.praisecount + '</span>' +
                        '<img onclick="praiseevalute(this,\'' + this.evaluate_id + '\', \'' + this.ispraise + '\');" src="' + imgurl + '">' +
                        '</div>' +
                        '</div>' +
                        '<br/>' +
                        '<span class="item_comment_desc">' + this.evaluate_desc + '</span>' +
                        '<span class="item_time">2016-12-05</span>';
                    $(reply).each(function () {
                        evaluateinfo += '<div class="guest_service_bg">' +
                            '<span class="home">摘记回复:</span>' +
                            '<span class="message">' + this.reply_text + '</span>' +
                            '</div>'
                    });
                    evaluateinfo +=
                        '</div>' +'<hr style="height: 1px; width: 90%; float: right; background-color: #f4f4f4; border: none;margin-top: 11px;"/>'+
                        '</div>' +
                        '</div>'
                        ;
                });
                $("#evaluate").append(evaluateinfo);
                if (evaluatelength >= 3) {
                    $("#evaluatemore").show();
                    $("#nullmore").hide();
                } else {
                    $("#evaluatemore").hide();
                    $("#nullmore").show();
                }
                remove_advertising();
            } else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show();
            $("#hint_message").html("网络异常");
        }
    });
});
function back() {
    window.history.back(-1);
}

function isempty(str) {
    return str == undefined || str == null || str == "" || str == "null";
}


function sendEvaluate() {
    var storyid = $("#storyid").val();
    var shopid = $("#shopid").val();
    var evaluatetext = $("#evaluatetext").val();
    var token = $("#token").val();
    if(isempty(token))
        token="null";
    if (isempty(evaluatetext)) {
        $("#hintbg").show()
        $("#hint_message").html("请输入精选留言");
        return;
    }
    var url = "/except/shopcontroller/evaluate.action";
    var jsonstr = "{token:" + token + "}";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            storyid: storyid,
            text: evaluatetext,
            json: jsonstr
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            if (code == 20000) {
                $("#evaluatetext").val('');
                $("#hintbg").show()
                $("#hint_message").show().html("留言成功");
            } else if (code == 10001 || code == 10002) {
                location.href = "/except/login.jsp";
            } else {
                $("#hintbg").show()
                $("#hint_message").html(message);
            }
        },
        error: function (data) {
            $("#hintbg").show()
            $("#hint_message").html("网络异常");
        }
    });
}

function look_more() {
    var storyid = $("#storyid").val();
    location.href = '/except/files/more_evaluate.jsp?storyid=' + storyid;
}

function praiseevalute(img, evaluate_id, ispraise) {
    var token = $("#token").val();
    if (ispraise == "true") {
        $("#hintbg").show()
        $("#hint_message").html("您已点赞，请勿重复操作");
        return;
    } else {
        var url = "/except/shopcontroller/praise.action";
        if(isempty(token))
            token="null";
        var jsonstr = "{token:" + token + "}";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                evaluateid: evaluate_id,
                json: jsonstr
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                if (code == 20000) {
                    $("#hintbg").show()
                    $("#hint_message").html("点赞成功");
                    var praisecount = $("#praise" + evaluate_id).html();
                    $("#praise" + evaluate_id).html(praisecount * 1 + 1);
                    $(img).attr("src", "img/praise_red.png");
                } else if (code == 10001 || code == 10002) {
                    location.href = "/except/login.jsp";
                } else{
                    $("#hintbg").show()
                    $("#hint_message").html(message);
                }
            },
            error: function (data) {
                $("#hintbg").show()
                $("#hint_message").html("网络异常");
            }
        });
    }
}